package com.ssyc.gsk_teacher.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.AttenInfo;

import java.util.List;

/**
 * 考勤管理的列表适配器
 * Created by Administrator on 2018/7/1 0001.
 */

public class AttenRvAdapter extends BaseQuickAdapter<AttenInfo.ListBean, BaseViewHolder> {
    private Context context;
    private Handler handler;

    public AttenRvAdapter(Context context, int layoutResId, @Nullable List<AttenInfo.ListBean> data) {
        super(layoutResId, data);
        this.context = context;
        handler = new Handler();
    }

    @Override
    protected void convert(BaseViewHolder helper, AttenInfo.ListBean item) {
        //头像
        RoundedImageView ivHead = helper.getView(R.id.iv_head);
        ImageUtil.displayImage(context, ivHead, item.getIcon());

        //姓名
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getName());

        //已到,迟到,请假,旷课的线性布局
        LinearLayout llyd = helper.getView(R.id.ll_yd);
        LinearLayout llcd = helper.getView(R.id.ll_cd);
        LinearLayout llqj = helper.getView(R.id.ll_qj);
        LinearLayout llkk = helper.getView(R.id.ll_kk);

        //已到,迟到,请假,旷课的图片
        ImageView ivyd = helper.getView(R.id.iv_yd);
        ImageView ivcd = helper.getView(R.id.iv_cd);
        ImageView ivqj = helper.getView(R.id.iv_qj);
        ImageView ivkk = helper.getView(R.id.iv_kk);

        //根据状态设置状态值,用于标记哪个按钮被选中1,2,3,4分别对应已到,迟到,请假,旷到,0表示都不选中,具体的值需要根据服务器返的状态来决定
        if ("0".equals(item.getATTENDANCE_TYPE())) {
            //已到被选中
            ivyd.setImageResource(R.drawable.teacher_img_selet);
            ivcd.setImageResource(R.drawable.teacher_img_normal);
            ivqj.setImageResource(R.drawable.teacher_img_normal);
            ivkk.setImageResource(R.drawable.teacher_img_normal);
        } else if ("1".equals(item.getATTENDANCE_TYPE())) {
            //迟到被选中
            ivyd.setImageResource(R.drawable.teacher_img_normal);
            ivcd.setImageResource(R.drawable.teacher_img_selet);
            ivqj.setImageResource(R.drawable.teacher_img_normal);
            ivkk.setImageResource(R.drawable.teacher_img_normal);
        } else if ("2".equals(item.getATTENDANCE_TYPE())) {
            //请假被选中
            ivyd.setImageResource(R.drawable.teacher_img_normal);
            ivcd.setImageResource(R.drawable.teacher_img_normal);
            ivqj.setImageResource(R.drawable.teacher_img_selet);
            ivkk.setImageResource(R.drawable.teacher_img_normal);
        } else if ("3".equals(item.getATTENDANCE_TYPE())) {
            //旷课被选中
            ivyd.setImageResource(R.drawable.teacher_img_normal);
            ivcd.setImageResource(R.drawable.teacher_img_normal);
            ivqj.setImageResource(R.drawable.teacher_img_normal);
            ivkk.setImageResource(R.drawable.teacher_img_selet);
        } else if ("".equals(item.getATTENDANCE_TYPE())) {
            //都不选中
            ivyd.setImageResource(R.drawable.teacher_img_normal);
            ivcd.setImageResource(R.drawable.teacher_img_normal);
            ivqj.setImageResource(R.drawable.teacher_img_normal);
            ivkk.setImageResource(R.drawable.teacher_img_normal);
        }

        //设置点击事件
        llyd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //已到的点击事件
                if (!"0".equals(item.getATTENDANCE_TYPE())) {
                    item.setATTENDANCE_TYPE("0");
                    notifyRefresh(helper.getLayoutPosition());
                }
            }
        });

        llcd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!"1".equals(item.getATTENDANCE_TYPE())) {
                    item.setATTENDANCE_TYPE("1");
                    notifyRefresh(helper.getLayoutPosition());
                }
            }
        });

        llqj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!"2".equals(item.getATTENDANCE_TYPE())) {
                    item.setATTENDANCE_TYPE("2");
                    notifyRefresh(helper.getLayoutPosition());
                }
            }
        });

        llkk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!"3".equals(item.getATTENDANCE_TYPE())) {
                    item.setATTENDANCE_TYPE("3");
                    notifyRefresh(helper.getLayoutPosition());
                }
            }
        });

    }

    /**
     * 在item点击事件中直接使用notifyDataSetChanged()
     * 解决java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
     *
     * @param pos
     */
    private void notifyRefresh(final int pos) {
        Runnable r = new Runnable() {
            public void run() {
                notifyItemChanged(pos);
            }
        };
        handler.post(r);
    }

    /**
     * 提供给Activity释放handler对象的方法,防止内存泄漏
     */
    public void releaseHandler() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
